import queue
import random
import threading

import requests

url_list = ["https://www.cnblogs.com/#p{}".format(i) for i in range(1, 50 + 1)]


def get_data(url):
    res = requests.request("GET", url)
    return res.text


def set_data(html_text):
    name = random.randint(1000, 99999)
    name = str(name)
    with open("./file/{}.html".format(name), 'w', encoding="utf-8") as f:
        f.write(html_text)


def put_msg(html_queue: queue.Queue):
    for url in url_list:
        msg = get_data(url)
        print("1")
        html_queue.put(msg)


def get_msg(html_queue: queue.Queue):
    while True:
        msg = html_queue.get()
        print("2")
        set_data(msg)


def run():
    html_queue = queue.Queue()

    put_msg(html_queue)

    for idx in range(2):
        thread = threading.Thread(target=get_msg, args=(html_queue,))
        thread.start()


if __name__ == '__main__':
    run()
